package com.example.certificate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.dto.CardBasePageDto;
import com.example.certificate.dto.InsRoleDto;
import com.example.certificate.dto.common.DisableStatDto;
import com.example.certificate.entity.SysRole;
import com.example.certificate.entity.User;
import com.example.certificate.mapper.SysRoleMapper;
import com.example.certificate.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.certificate.utils.NodeTreeUtil;
import com.example.certificate.utils.ResultConstant;
import com.example.certificate.utils.ResultEntity;
import com.example.certificate.vo.CascadeCardVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统角色表 服务实现类
 * </p>
 *
 * @author hao
 * @since 2022-02-23 17:13:12
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;


    @Override
    public ResultEntity insRole(InsRoleDto dto, User user) {
        SysRole role = new SysRole();
        BeanUtils.copyProperties(dto,role);
        return sysRoleMapper.insert(role) > 0 ? new ResultEntity() : new ResultEntity(ResultConstant.ADD_FAIL.resultCode, "操作失败");
    }


    @Override
    public ResultEntity selRoleTree() {
        return new ResultEntity(NodeTreeUtil.sysRolesTress(sysRoleMapper.selectList(new QueryWrapper<SysRole>())));
    }

    @Override
    public Page<SysRole> selRolePage(CardBasePageDto dto) {
        Page<SysRole> page = new Page<>();
        page.setCurrent(dto.getCurrentPage() == null ? 1 : dto.getCurrentPage());
        page.setSize(dto.getPageSize() == null ? 10 : dto.getPageSize());
        return sysRoleMapper.selPage(page,dto.getCardStatus(),dto.getContent());
    }

    @Override
    public ResultEntity updateStatus(DisableStatDto dto) {
        if(sysRoleMapper.updateRoleStatus(dto.getId(),dto.getStatus()) > 0){
            return new ResultEntity();
        }
        return new ResultEntity(ResultConstant.EDIT_FAIL);
    }

    @Override
    public List<CascadeCardVo> cascadeRole() {
        List<CascadeCardVo> roleCascade = sysRoleMapper.getRoleCascade();
        Map<String, List<CascadeCardVo>> collect = roleCascade.stream()
                .sorted(Comparator.comparing(s -> String.valueOf(s.getSort())))
                .collect(Collectors.groupingBy(CascadeCardVo::getParentId));
        Iterator<CascadeCardVo> iterator = roleCascade.stream().iterator();
        while (iterator.hasNext()){
            CascadeCardVo next = iterator.next();
            next.setChildren(collect.get(next.getValue()));
        }
        return roleCascade.stream().filter(s -> s.getParentId().equals("0")).collect(Collectors.toList());
    }

}
